package com.dmooo.yxmmerchant.view.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.common.dialog.MyDialog;
import com.common.glide.GlideUtils;
import com.common.util.LogUtil;
import com.common.util.ScreenUtil;
import com.common.util.ToastUtil;
import com.cretin.www.clearedittext.view.ClearEditText;
import com.dmooo.yxmmerchant.R;
import com.dmooo.yxmmerchant.base.BaseActivity;
import com.dmooo.yxmmerchant.common.Config;
import com.dmooo.yxmmerchant.util.ImageCompress;
import com.dmooo.yxmmerchant.util.SelectPicUtil;
import com.dmooo.yxmmerchant.view.contract.UserSetContract;
import com.dmooo.yxmmerchant.view.presenter.UserSetPrestener;
import com.hjq.permissions.Permission;
import com.zhihu.matisse.Matisse;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;
import top.limuyang2.customldialog.BottomTextListDialog;
import top.limuyang2.customldialog.adapter.BottomTextListAdapter;

public class IdentificationActivity extends BaseActivity<UserSetPrestener> implements UserSetContract.UserSetView {
    @BindView(R.id.ll_add_photo)
    LinearLayout llAddPhoto;
    @BindView(R.id.img_id_front)
    ImageView imgIdFront;
    @BindView(R.id.ll_add_photo_back)
    LinearLayout llAddPhotoBack;
    @BindView(R.id.img_id_back)
    ImageView imgIdBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.img_del_front)
    ImageView imgDelFront;
    @BindView(R.id.img_del_back)
    ImageView imgDelBack;
    @BindView(R.id.et_name)
    ClearEditText etName;
    @BindView(R.id.et_num)
    ClearEditText etNum;
    private File front_file, back_file;
    private int currentPic=1;
    BottomTextListDialog picDialog;
    @Override
    protected int getLayout() {
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
            setStatusBar(Color.WHITE);
        }
        return R.layout.activity_identification;
    }

    @Override
    protected void initEventAndData() {
        tvTitle.setText("商户实名认证");
        picDialog= MyDialog.showSheetDialog(getSupportFragmentManager(), new BottomTextListAdapter.OnItemClickListener() {
            @Override
            public void onClick(View view, int i) {
                switch (i){
                    case 0:
                        if (!checkPermission(Permission.READ_PHONE_STATE)) {
                            requestPermission(Permission.READ_PHONE_STATE, "手机存储权限");
                            return;
                        }
                        SelectPicUtil.selectPic(IdentificationActivity.this, 1, Config.SELECT_PHOTO);
                        break;
                    case 1:
                        if (!checkPermission(Permission.CAMERA)) {
                            requestPermission(Permission.CAMERA, "拍照权限");
                            return;
                        }
                        SelectPicUtil.selectPic(IdentificationActivity.this, 1, Config.TAKE_PHOTO);
                        break;
                }
                picDialog.dismiss();
            }
        },new ArrayList<>(Arrays.asList("从相册获取","拍照")));
    }

    @Override
    protected void createPresenter() {
        mPresenter=new UserSetPrestener(this,this);
    }

    @OnClick({R.id.tv_left, R.id.txt_up_front, R.id.txt_up_back,R.id.img_del_front,R.id.img_del_back,R.id.btn_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_left:
                finish();
                break;
            case R.id.txt_up_front:
            case R.id.txt_up_back:
                currentPic=(view.getId()==R.id.txt_up_front?1:2);
                picDialog.show();
                break;
            case R.id.img_del_front:
                llAddPhoto.setVisibility(View.VISIBLE);
                front_file=null;
                imgDelFront.setVisibility(View.GONE);
                imgIdFront.setImageDrawable(null);
                break;
            case R.id.img_del_back:
                llAddPhotoBack.setVisibility(View.VISIBLE);
                back_file=null;
                imgDelBack.setVisibility(View.GONE);
                imgIdBack.setImageDrawable(null);
                break;
            case R.id.btn_ok:
                if(TextUtils.isEmpty(etName.getText().toString())){
                    ToastUtil.getInstance().showErrorMsg("请输入真实姓名");
                    return;
                }
                if(TextUtils.isEmpty(etNum.getText().toString())){
                    ToastUtil.getInstance().showErrorMsg("请输入身份证号");
                    return;
                }
                if(front_file==null){
                    ToastUtil.getInstance().showErrorMsg("请上传身份证正面照");
                    return;
                }
                if(back_file==null){
                    ToastUtil.getInstance().showErrorMsg("请上传身份证反面照");
                    return;
                }
                mPresenter.userAuth(token,front_file,back_file,etName.getText().toString().trim(),etNum.getText().toString().trim());
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode ==  Config.SELECT_PHOTO && resultCode == RESULT_OK) {
            if(currentPic==1){
                GlideUtils.showImageViewToRound(this, getResources().getDrawable(R.mipmap.defaultpicture_classify), getRealFilePath(this, Matisse.obtainResult(data).get(0)), imgIdFront, 10);
                llAddPhoto.setVisibility(View.GONE);
                front_file = new File(getRealFilePath(this, Matisse.obtainResult(data).get(0)));
                imgDelFront.setVisibility(View.VISIBLE);
            }else{
                GlideUtils.showImageViewToRound(this, getResources().getDrawable(R.mipmap.defaultpicture_classify), getRealFilePath(this, Matisse.obtainResult(data).get(0)), imgIdBack, 10);
                llAddPhotoBack.setVisibility(View.GONE);
                back_file = new File(getRealFilePath(this, Matisse.obtainResult(data).get(0)));
                imgDelBack.setVisibility(View.VISIBLE);
            }
        } else if (requestCode ==  Config.TAKE_PHOTO && resultCode == RESULT_OK) {
            Bitmap bitmap = null;
            File file = this.getFilesDir();
            String path = file.getPath();
            try {
                bitmap = ImageCompress.getBitmapFormUri(this, SelectPicUtil.tempUri);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String tempPath=ImageCompress.savePhoto(bitmap, path, System.currentTimeMillis()+"icon");
            LogUtil.d("dsf",tempPath);
            if(currentPic==1){
                GlideUtils.showImageViewToRound(this, getResources().getDrawable(R.mipmap.defaultpicture_classify), tempPath, imgIdFront, ScreenUtil.dp2px(this,5.0f));
                llAddPhoto.setVisibility(View.GONE);
                front_file = new File(tempPath);
                imgDelFront.setVisibility(View.VISIBLE);
            }else{
                GlideUtils.showImageViewToRound(this, getResources().getDrawable(R.mipmap.defaultpicture_classify),tempPath, imgIdBack,  ScreenUtil.dp2px(this,5.0f));
                llAddPhotoBack.setVisibility(View.GONE);
                back_file = new File(tempPath);
                imgDelBack.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public void editInfoSuccess() {

    }

    @Override
    public void changePwdSuccess() {

    }

    @Override
    public void editAvatarSuccess() {

    }

    @Override
    public void userAuthSuccess() {
        ToastUtil.getInstance().showSuccessMsg("认证提交成功,请等待审核");
        finish();
    }
}
